package com.dailyyoga.res;

import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;

public class YogaResManager {

	YogaRes mYogaRes;
	Context mContext;
	String mPath = "ZIP";

	static YogaResManager mYogaResManager;

	public static YogaResManager getInstance(Context context) {

		synchronized ("YogaResManager") {

			if (mYogaResManager == null) {
				mYogaResManager = new YogaResManager();
				mDefultLang = null;
			}
			mYogaResManager.mContext = context;

			if (mYogaResManager.getLang().equals("ko_KR_")) {
				mYogaResManager.mPath = "ZIP";
			} else {
				mYogaResManager.mPath = "APK";
			}

			if (mYogaResManager.mPath.equals("APK")) {
				mYogaResManager.mYogaRes = new YogaPlugAppRes(context,
						mYogaResManager.getLang());
			} else if (mYogaResManager.mPath.equals("ZIP")) {
				mYogaResManager.mYogaRes = new YogaFileRes(
						mYogaResManager.getLang());
			}
			return mYogaResManager;
		}
	}

	public String getServercKey() {
		return "zh";
	}

	public void setLang(String lang) {
		SharedPreferences preferences = mContext.getSharedPreferences(
				"YogaResManager", 0);
		preferences.edit().putString("lang", lang).commit();
		mYogaResManager = null;
		getInstance(mContext);
	};

	private static String mDefultLang = null;

	public String getLang() {
		return "";
	}

	public void updateConfigLang() {

		Resources resources = mContext.getResources();

		Configuration config = resources.getConfiguration();

		DisplayMetrics dm = resources.getDisplayMetrics();

		String lang = getLang();

		if (lang.equals("zh_CN_")) {
			config.locale = Locale.SIMPLIFIED_CHINESE;
		} else if (lang.equals("zh_TW_")) {
			config.locale = Locale.TRADITIONAL_CHINESE;
		} else if (lang.equals("ja_JP_")) {
			config.locale = new Locale("ja");
		} else if (lang.equals("en_")) {
			config.locale = new Locale("en");
		} else if (lang.equals("ko_KR_")) {
			config.locale = new Locale("ko");
		}

		resources.updateConfiguration(config, dm);

	}

	public Locale getLocale() {

		String lang = getLang();

		if (lang.equals("zh_CN_")) {
			return Locale.SIMPLIFIED_CHINESE;
		} else if (lang.equals("zh_TW_")) {
			return Locale.TRADITIONAL_CHINESE;
		} else if (lang.equals("ja_JP_")) {
			return new Locale("ja");
		} else if (lang.equals("en_")) {
			return new Locale("en");
		} else if (lang.equals("ko_KR_")) {
			return new Locale("ko");
		}
		return null;
	}

	public void setDataPath(String path) {
		mPath = path;
	}

	public String getDataPath() {
		return mPath;
	}

	public String getString(String plugName, String key) {
		return mYogaRes.getString(plugName, key);
	}

	public Bitmap getBitMap(String plugName, String key) {
		return mYogaRes.getBitMap(plugName, key);
	}

	public FileDescriptorEx getPlayAudioPath(String plugName, String key) {
		return mYogaRes.getPlayAudioPath(plugName, key);
	}

	public FileDescriptorEx getVideoPath(String plugName, String key) {

		return mYogaRes.getVideoPath(plugName, key);
	}

	public boolean isInstallPlugs(String plugId) {
		return mYogaRes.isInstallPlugs(plugId);
	};

	public InputStream getParamXMl(String plugName, String praramKey)
			throws IOException {

		return mYogaRes.getParamXMl(plugName, praramKey);
	}

	public Bitmap getPrompt(String plugName, String key) {
		return mYogaRes.getPrompt(plugName, key);
	};

	public int getPlugVc(String plugName) {

		return mYogaRes.getPlugVc(plugName);
	};

	public void reset() {

		synchronized ("YogaResManager") {
			mYogaResManager = null;
			mDefultLang = null;
			System.gc();
			Log.d("mDefultLang", "release" + mDefultLang);
		}
	}

}
